var db = require('../db/commonDB')

// 根据关键词查找商品
module.exports = {
    //通过关键字搜索商品
    getProductByKeywords: function (goods, cb) {
        var sqlQuery = "select * from goods where goods_title like ? and is_delete = 0;"
        var paramsQuery = `%${goods.goods_title}%`
        db(sqlQuery, paramsQuery, function (err, results) {
            if (err) {
                cb(err, null)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('查找商品失败！').message)
                }
            }
        })
    },
    //通过多个关键词搜索
    getProductByMoreKeywords:function(keys,cb){
        var sql = `select * from goods where goods_title like "%"?"%" and goods_title like "%"?"%" and goods_title like "%"?"%" and goods_title like "%"?"%"`
        var params = [keys.k1,keys.k2,keys.k3,keys.k4]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err, null)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('查找商品失败！').message)
                }
            }
        })
    },
    //获取所有商品
    getAllProducts: function (cb) {
        var sql = `select * from goods where is_delete = 0`
        db(sql, {}, function (err, results) {
            if (err) {
                cb(err, null)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('获取商品失败'))
                }
            }
        })
    },

    //添加商品
    addProducts: function (goods, cb) {
        var sql = `insert into goods(goods_id,goods_title) values(?,?)`
        var params = [goods.goods_id, goods.goods_title]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results) //增加商品成功
                } else {
                    cb(new Error('添加商品失败！'))
                }
            }
        })
    },
    //通过商品id获取商品
    getProductById: function (goods, cb) {
        var sql = `select * from goods where goods_id = ? and is_delete = 0`
        var params = goods.goods_id
        db(sql, params, function (err, results) {
            if (err) {
                cb(err, null)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('获取商品失败'))
                }
            }
        })
    },
    //通过类型获取商品
    getProductByType: function (goods, cb) {
        var sql = `select * from goods where goods_type = '${goods.goods_type}' and is_delete = 0`
        var params = goods.goods_type
        db(sql, params, function (err, results) {
            if (err) {
                cb(err, null)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('获取商品失败'))
                }
            }
        })
    },
    //删除商品
    delProducts: function (goods, cb) {
        var sql = `update goods set is_delete = 1 where goods_id = ?`
        var params = goods.goods_id
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('删除商品失败！'))
                }
            }
        })
    },
    //修改商品
    changeProducts: function (goods, cb) {
        var sql = `update goods set goods_title = ?,goods_details = ?,goods_price = ?,goods_counts = ?,goods_score = ?,goods_type = ? where goods_id = ?`
        var params = [goods.goods_title, goods.good_details, goods.goods_price, goods.goods_counts, goods.goods_score, goods._goods_type, goods.goods_id]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('修改商品失败！'))
                }
            }
        })
    },
    //根据发行时间获取商品
    getProductByDate: function (cb) {
        var sql = `select * from goods WHERE goods_date <> 'null' order by goods_date DESC`
        db(sql, {}, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('获取商品失败！'))
                }
            }
        })
    },
    //根据商品折扣获取商品
    getProductByDiscount: function (cb) {
        var sql = `select * from goods WHERE goods_discount = 1`
        db(sql, {}, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('获取商品失败！'))
                }
            }
        })
    },
    //根据销量获取商品
    getProductBySales: function (cb) {
        var sql = `select * from goods order by goods_sales DESC`
        db(sql,{},function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('获取商品失败！'))
                }
            }
        })
    },
    //根据收藏数量获取商品
    getProductByCollection: function (cb) {
        var sql = `select * from goods order by goods_collection DESC`
        db(sql, {}, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('获取商品失败！'))
                }
            }
        })
    },
    //根据浏览数量获取商品
    getProductByBrowse: function (cb) {
        var sql = `select * from goods order by goods_browse DESC`
        db(sql, {}, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('获取商品失败！'))
                }
            }
        })
    },
    //添加购物车
    addShopCart: function (shopcart, cb) {
        var sql = `insert into shopcart(shopcart_count,shopcart_attribute,shopcart_goodsID,user_id,shopcart_addTime,shopcart_goods_img,shopcart_goods_price,shopcart_goods_title,shopcart_goods_size) value(?,?,?,?,?,?,?,?,?)`
        var params = [shopcart.shopcart_count, shopcart.attribute, shopcart.shopcart_goodsID, shopcart.user_id, shopcart.shopcart_addTime,shopcart.shopcart_goods_img,shopcart.shopcart_goods_price,shopcart.shopcart_goods_title,shopcart.shopcart_goods_size]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('添加购物车失败！'))
                }
            }
        })
    },
    //删除购物车
    delShopCart: function (shopcart, cb) {
        var sql = `delete from shopcart where shopcart_id = ?`
        var params = shopcart.shopcart_id
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('删除购物车失败！'))
                }
            }
        })
    },
    //根据用户ID查看购物车
    viewShopCart:function(shopcart,cb){
        var sql = `select * from shopcart where user_id = ?`
        var params = shopcart.user_id
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('查看购物车失败！'))
                }
            }
        })
    },
    //获取商品更多图片信息
    getMoreProductPic: function () {

    }
}